package com.runen.wnhz.runen.ui.adapter.puadapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.ruffian.library.RTextView;
import com.runen.wnhz.runen.R;
import com.runen.wnhz.runen.bean.LessonSmallSectionBean;
import com.runen.wnhz.runen.bean.UpdateSmallSelectionPlay;
import com.runen.wnhz.runen.common.utils.Base64Utils;
import com.runen.wnhz.runen.ui.adapter.puadapter.SmallSlectionRecyclerAdapter.ViewHolder;

import org.simple.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SmallSlectionRecyclerAdapter extends RecyclerView.Adapter<ViewHolder> {

    private List<LessonSmallSectionBean.DataBean> dataBeans;

    private Context context;

    public SmallSlectionRecyclerAdapter(List<LessonSmallSectionBean.DataBean> dataBeans, Context context) {
        this.dataBeans = dataBeans;
        this.context = context;
        EventBus.getDefault().register(this);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.small_selection_item_layout, viewGroup,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        final LessonSmallSectionBean.DataBean dataBean = dataBeans.get(position);
        viewHolder.rTextView.setText(dataBean.getN_name());
        viewHolder.relative_smallSelection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    UpdateSmallSelectionPlay updateSmallSelectionPlay = new UpdateSmallSelectionPlay();
                    String base64videoUrl = Base64Utils.decode(dataBean.getTv_url());
                    updateSmallSelectionPlay.setVideoUrl(base64videoUrl.substring(3, base64videoUrl.length() - 5));
                    if ("1".equals(dataBean.getIs_collect())) {
                        updateSmallSelectionPlay.setCollected(true);
                    } else {
                        updateSmallSelectionPlay.setCollected(false);
                    }
                    updateSmallSelectionPlay.setPosition(position);
                    EventBus.getDefault().post(updateSmallSelectionPlay);
            }
        });
    }

    @Override
    public int getItemCount() {
        if(dataBeans != null){
            return dataBeans.size();
        }else{
            return 0;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.rTextView)
        RTextView rTextView;

        @BindView(R.id.relative_smallSelection)
        RelativeLayout relative_smallSelection;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
